package littlecyberwar.tool;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;

import littlecyberwar.ui.*;

public class MouseSidestepHandler extends MouseStraightHandler {

// a common superclass for different kind of mouse action to game command
// i.e. move straight ahead
// rotate
// side step, etc.
  public MouseSidestepHandler() {
    super();
    // startDragX = -1;
    // startDragY = -1;
    id = H_SIDESTEP;
	unitSelectedText = "Drag mouse to left or right of unit and release button to sidestep unit.";
	noUnitSelectedText = "Click on a unit to select for sidestep movement.";
  }

  protected void dragSelected( Point newP ) {
	selectedTrans = ((SelectedElementContainer)selectedElements.get(0)).element.getTransform();

	try {
	  endDragP = selectedTrans.inverseTransform(newP, null);
	} catch ( Exception ex ) {
	  ex.printStackTrace();
	}
	// now figure out just the Y difference between the 2 points
	// double diffY = endDragP.getY() - startDragP.getY();
	double diffX = endDragP.getX() - startDragP.getX();

	AffineTransform currTrans;
	Shape originalRec;
//	selectedArea = new Area();
	
	for (int i=0; i < selectedElements.size(); i++ ){
		SelectedElementContainer selEl = (SelectedElementContainer)selectedElements.get(i);
		Element el = selEl.element;
		AffineTransform trans= el.getTransform();
		currTrans = (AffineTransform)trans.clone();
		currTrans.translate(diffX, 0);

		originalRec = el.getRectangle();
		selEl.selectedRectangle = currTrans.createTransformedShape(originalRec);
		selEl.currTrans = currTrans;
	
//		Rectangle2D rec2update = selEl.selectedRectangle.getBounds2D();
//		updateSelectedAreaForRepaint(rec2update);
		
		DirtyAreaManager.addDirtyArea(selEl.selectedRectangle);
			
	}
	pixMoved = Math.abs(diffX);
  }
  
//////////////////////////////////////////////////////////////////////
//
//
//    // attempt to use transform on point to see if we can change
//    // coordinate to conform to that of the new user space,
//    // then just translate from that
//    //
//    try {
//      endDragP = selectedTrans.inverseTransform(newP, null);
//    } catch ( Exception ex ) {
//      ex.printStackTrace();
//    }
//
//    //System.out.println("newP x=" + newP.getX() + ", y=" + newP.getY() );
//
//    // now figure out just the Y difference between the 2 points
//    double diffX = endDragP.getX() - startDragP.getX();
//    //System.out.println("transformed diffY= " + diffY );
//    currTrans = (AffineTransform)selectedTrans.clone();
//    currTrans.translate(diffX, 0);
//    pixMoved = Math.abs(diffX);
//
//    selectedRectangle = currTrans.createTransformedShape(originalRec);
//    return;
//  }

}